/*
 This file is part of BTStracker
		
    BTStracker is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BTStracker is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.	
	author: azbest.pro (azbest.pro@gmail.com)
*/
package pl.vdl.azbest.mremote.gui.composite;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.InvalidPropertiesFormatException;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.prefs.Preferences;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Label;

import pl.vdl.azbest.log.gui.composite.CompositeSystemConsole;
import pl.vdl.azbest.mremote.Conf;
import pl.vdl.azbest.mremote.cmd.Cmd_saveStringToFile;
import pl.vdl.azbest.mremote.cmd.Command;
import pl.vdl.azbest.mremote.cmd.Invoker;
import pl.vdl.azbest.mremote.gui.GUIFacade;
import pl.vdl.azbest.mremote.probe.signal.CellSignalData;

public class CompositeCellSignalTable extends Composite {

	private Table table = null;

	private Group group = null;

	private Button button3 = null;

	public CompositeCellSignalTable(Composite parent, int style) {
		super(parent, style);
		initialize();
	}

	private void initialize() {
		GridData gridData4 = new GridData();
		gridData4.horizontalSpan = 9;
		GridData gridData11 = new GridData();
		gridData11.horizontalSpan = 5;
		GridLayout gridLayout1 = new GridLayout();
		gridLayout1.numColumns = 9;
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalSpan = 9;
		gridData.verticalAlignment = GridData.FILL;
		table = new Table(this, SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLayoutData(gridData);
		table.setLinesVisible(true);

		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(60);
		tableColumn.setText("Time");
		TableColumn tableColumn1 = new TableColumn(table, SWT.NONE);
		tableColumn1.setWidth(60);
		tableColumn1.setText("Signal");
		TableColumn tableColumn2 = new TableColumn(table, SWT.NONE);
		tableColumn2.setWidth(60);
		tableColumn2.setText("LAI");
		TableColumn tableColumn3 = new TableColumn(table, SWT.NONE);
		tableColumn3.setWidth(60);
		tableColumn3.setText("CellID");
		TableColumn tableColumn4 = new TableColumn(table, SWT.NONE);
		tableColumn4.setWidth(60);
		tableColumn4.setText("GPS data");

		createGroup();

		this.setLayout(gridLayout1);
		this.setSize(new Point(517, 394));

		label2 = new Label(this, SWT.NONE);
		label2
				.setText("Edit XML Properities files. Appliaction have ti use \"Element\" name to read in config value");
		label2.setLayoutData(gridData4);
		init();
	}

	/**
	 * This method initializes group
	 * 
	 */
	private void createGroup() {
		GridData gridData5 = new GridData();
		gridData5.verticalSpan = 2;
		gridData5.verticalAlignment = GridData.FILL;
		gridData5.grabExcessHorizontalSpace = false;
		gridData5.grabExcessVerticalSpace = true;
		gridData5.horizontalAlignment = GridData.CENTER;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		GridData gridData1 = new GridData();
		gridData1.horizontalAlignment = GridData.FILL;
		gridData1.grabExcessHorizontalSpace = true;
		gridData1.grabExcessVerticalSpace = false;
		gridData1.horizontalSpan = 9;
		gridData1.verticalAlignment = GridData.FILL;
		group = new Group(this, SWT.NONE);
		group.setLayoutData(gridData1);
		group.setLayout(gridLayout);
		button5 = new Button(group, SWT.NONE);

		button5.setLayoutData(gridData5);

		button5.setText("Save");

		button5
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(
							org.eclipse.swt.events.SelectionEvent e) {

						TableItem it = table.getItem(selectedIndex);
						FileDialog fd = new FileDialog(
								CompositeCellSignalTable.this.getShell(),
								SWT.SAVE);
						fd.setFilterExtensions(new String[] { ".txt" });
						fd.open();

						String path = fd.getFilterPath()
								+ Conf.getInstance().PathS + fd.getFileName();

						Command c = new Cmd_saveStringToFile(
								getCellSignalData(), path);
						Invoker.getInstance().addCommand(c);
						Invoker.getInstance().execute();

					}
				});

	}

	private void init() {
		GUIFacade.getInstance().addComposite(this);
	}

	private void addRow() {
		TableItem item = new TableItem(table, SWT.NULL);
		item.setText(0, "Time");
		item.setText(1, "Signal");
		item.setText(1, "LAI");
		item.setText(1, "CellID");
		item.setText(1, "GPS");
	}

	private int selectedIndex;

	private Button button5 = null;

	private Label label2 = null;

	public void createLangFile(String path) throws IOException {
		File f = new File(path);
		// if(!f.exists()) f.createNewFile();
		// if(!f.canWrite()) throw new IOException("Cant write to file :"+path);

		Properties prop = new Properties();
		TableItem items[] = table.getItems();
		for (TableItem it : items) {
			prop.setProperty(it.getText(0), it.getText(1));

		}

		prop.storeToXML(new FileOutputStream(f), "Internationalizaton");

	}

	public void loadFile(String path) throws InvalidPropertiesFormatException,
			IOException {
		TableItem[] items = table.getItems();
		for (TableItem i : items) {
			i.dispose();
		}

		File f = new File(path);
		// //System.out.println(path);
		Properties prop = new Properties();
		prop.loadFromXML(new FileInputStream(f));
		Set kset = prop.keySet();
		Iterator it = kset.iterator();
		// System.out.println("kset size> "+kset.size());
		// System.out.println("kset size> "+kset.size());
		while (it.hasNext()) {
			TableItem item = new TableItem(table, SWT.NULL);
			String key = (String) it.next();
			String property = prop.getProperty(key);
			item.setText(0, key);
			item.setText(1, property);

		}
		TableColumn[] col = table.getColumns();
		for (TableColumn tableColumn1 : col) {
			tableColumn1.pack();
		}

	}

	public void loadCellSignalData(final CellSignalData data) {
		getDisplay().syncExec(new Runnable(){
			public void run() {
				TableItem item = new TableItem(table, SWT.NULL);
				item.setText(0, data.getTime());
				item.setText(1, data.getSignal());
				item.setText(2, data.getLAI());
				item.setText(3, data.getCI());
				TableColumn[] col = table.getColumns();
				for (TableColumn tableColumn1 : col) {
					tableColumn1.pack();
				}
			}
		});
	
	}

	private String getCellSignalData() {
		String output = "";
		TableItem items[] = table.getItems();
		for (TableItem it : items) {
			output += it.getText(0) + ",";
			output += it.getText(1) + ",";
			output += it.getText(2) + ",";
			output += it.getText(3) + Conf.getInstance().LineS;
		}
		return output;
	}
} // @jve:decl-index=0:visual-constraint="32,36"
